<#assign className = model.className>
<#assign classNameLower = className?uncap_first>
<#assign pkPropName = model.pkProp.name>
<#assign pkPropNameUp = model.pkProp.nameFirstUp>
package ${basepackage}.${moduleName}.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;

import org.springframework.stereotype.Controller;
import org.springframework.util.AutoPopulatingList;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import tso.action.BaseAction;
import tso.action.ParaMap;
import tso.dao.Page;
import tso.lang.BaseException;
import tso.spring.Sc;
import tso.sys.Security;
import tso.util.ValidateUtil;
//
import ${basepackage}.${moduleName}.model.${className};
import ${basepackage}.${moduleName}.service.I${className}Service;

/**
 * ${model.tableAlias} action
 * @author codeGener
 * @version 1.0
 * @created ${now?string("yyyy-MM-dd HH:mm")}
 */
@Controller
@RequestMapping("/${modulePath}/${classNameLower}Action.do")
public class ${className}Action extends BaseAction{
	//service
	private I${className}Service ${classNameLower}Service = (I${className}Service) Sc.getBean("${moduleName}.${className}Service");
	
	/**
	 * 初始化界面
	 * Description:界面权限数据初始化，下拉控件取值 
	 * @param enumType 下拉枚举值
	 * @param functionCode rone资源界面代码
	 * @param response    
	 * @return void 
	 */
	@RequestMapping(params = "action=format")
	public void format(String enumType,String functionCode,HttpServletResponse response) {
		try {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("permissionList", Security.getRight(functionCode));// 权限列表
			//TODO 赋值
			//...
			writeJson(true, resultMap, response);
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
	
	/**
	 * 查询
	 */
	@RequestMapping(params = "action=search${className}")
	<#if pageable==true>
	public void search${className}(ParaMap paraMap,int pageNumber,int pageSize,HttpServletResponse response){
		try {
			Map paramMap = paraMap.getMap();
			Page result = ${classNameLower}Service.find${className}PageBySearch(paramMap,pageNumber, pageSize);
			writeJson(true,result,response);
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
	<#else>
	public void search${className}(ParaMap paraMap,HttpServletResponse response){
		try {
			Map paramMap = paraMap.getMap();
			List result = ${classNameLower}Service.find${className}ListBySearch(paramMap);
			writeJson(true,result,response);
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
	</#if>
	
	
	/**
	 * 删除${model.tableAlias}
	 * Description:删除，逻辑删除 
	 * @param ids
	 * @param response
	 */
	@RequestMapping(params = "action=remove${className}")
	public void remove${className}(String[] ids,HttpServletResponse response){
		try {
			${classNameLower}Service.remove${className}(ids);
			writeJson(true,"删除成功",response);
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
	}
	
	
	/**
	 * 保存数据（新增或修改,复杂业务的话要拆开）
	 * @param 
	 * @param 
	 */
	@RequestMapping(params = "action=save${className}")
<#if model.subClazzes?size==0>
	public void save${className}(@ModelAttribute("model${className}")${className} ${classNameLower}, HttpServletResponse response){
		try {
			if(ValidateUtil.isEmpty(${classNameLower}.get${pkPropNameUp}())){
				${classNameLower}.set${pkPropNameUp}(null);
			}
			
			${classNameLower}Service.save${className}(${classNameLower});
			
			writeJson(true, "保存成功", response);			
		}catch (Exception e) {
			throw new BaseException(e, log);
		}	
	}
<#else>
	public void save${className}(@ModelAttribute("model${className}")${className} ${classNameLower}
	<#list model.subClazzes as subClazz>,String lst${subClazz.className}Json,String[] del${subClazz.className}Ids</#list>,HttpServletResponse response){
		try {
			if(ValidateUtil.isEmpty(${classNameLower}.get${pkPropNameUp}())){
				${classNameLower}.set${pkPropNameUp}(null);
			}
			<#list model.subClazzes as subClazz>
			List<${subClazz.className}> lst${subClazz.className} = JsonUtil.toList(lst${subClazz.className}Json,${subClazz.className}.class);
			</#list>
			${classNameLower}Service.save${className}(${classNameLower}<#list model.subClazzes as subClazz>,lst${subClazz.className},del${subClazz.className}Ids</#list>);
			writeJson(true, "保存成功", response);
		} catch (Exception e) {
			throw new BaseException(e, log);
		}
}
	
</#if>
	
	
	
	
	/**
	 * 执行方法前从后台加载数据
	 */
	@ModelAttribute("model${className}")
	public ${className} initAttribute(String action,String ${pkPropName},HttpServletResponse response){
		${className} ${classNameLower}=null;
		if (action.equals("save${className}")){
			if(ValidateUtil.isEmpty(${pkPropName})){
				${classNameLower} = new ${className}();
			}else{
				${classNameLower}=(${className})${classNameLower}Service.getObject(${pkPropName});					
			}
		}	
		return ${classNameLower};
	}
	
	
}